﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;
using MvcRags.Core;

namespace MvcRags.Pagination
{
    public static class PaginationHelper
    {
        public static MvcHtmlString PageSelectPanel(this HtmlHelper helper, int forcastPages = 2)
        {
            int page = 1;
            int pageCount = 1;

            object pageObj = helper.ViewContext.Controller.ViewData[PaginationSettings.ViewDataKeyPage];
            if (pageObj != null) page = (int) pageObj;

            object pageCountObj = helper.ViewContext.Controller.ViewData[PaginationSettings.ViewDataKeyPageCount];
            if (pageCountObj != null) pageCount = (int) pageCountObj;

            string prevClass = page <= 1 ? "disabled" : "";
            string nextClass = page >= pageCount ? "disabled" : "";

            int firstPage = page - forcastPages;
            int lastPage = page + forcastPages;

            if (pageCount < 5)
            {
                firstPage = 1;
                lastPage = pageCount;
            }
            else if (firstPage <= 1)
            {
                firstPage = 1;
                lastPage = forcastPages * 2 + 1;
            }
            else if (lastPage >= helper.ViewBag.PageCount)
            {
                lastPage = pageCount;
                firstPage = pageCount - forcastPages * 2;
            }
            else if (!(firstPage > 1 && lastPage < pageCount))
            {
                firstPage = 1;
                lastPage = pageCount;
            }

            string actionName = helper.GetActionName();

            StringBuilder output = new StringBuilder();
            output.AppendLine("<ul>");
            output.AppendLine(string.Format("<li class=\"pagination-btn pagination-first {0}\">{1}</li>", prevClass, helper.ActionLinkwParams("<<", actionName, GetRouteValueDict(PaginationSettings.RequestKeyPage, 1))));
            output.AppendLine(string.Format("<li class=\"pagination-btn pagination-prev {0}\">{1}</li>", prevClass, helper.ActionLinkwParams("<", actionName, GetRouteValueDict(PaginationSettings.RequestKeyPage, page - 1))));
            for (int i = firstPage; i <= lastPage; i++)
            {
                string c = i == page ? "active" : "";
                output.AppendLine(string.Format("<li class=\"pagination-btn {0}\">{1}</li>", c, helper.ActionLinkwParams(i.ToString(), "index", GetRouteValueDict(PaginationSettings.RequestKeyPage, i))));
            }
            output.AppendLine(string.Format("<li class=\"pagination-btn pagination-next {0}\">{1}</li>", nextClass, helper.ActionLinkwParams(">", actionName, GetRouteValueDict(PaginationSettings.RequestKeyPage, page + 1))));
            output.AppendLine(string.Format("<li class=\"pagination-btn pagination-last {0}\">{1}</li>", nextClass, helper.ActionLinkwParams(">>", actionName, GetRouteValueDict(PaginationSettings.RequestKeyPage, pageCount))));
            output.AppendLine("</ul>");
            return new MvcHtmlString(output.ToString());
        }

        private static RouteValueDictionary GetRouteValueDict(string propName, int value)
        {
            return new RouteValueDictionary() { { propName, value } };
        } 

        public static MvcHtmlString PageSizeSetLink(this HtmlHelper helper, string text, int pageSize, object htmlAttributes = null)
        {
            string actionName = helper.GetActionName();

            return helper.ActionLinkwParams(text, actionName,
                                   GetRouteValueDict(PaginationSettings.RequestKeyEntriesPerPage, pageSize));
        }
    }
}
